import { useState } from 'react';
import useMountData from './useMountData';

/**
 *
 * @param service 请求接口方法
 * @param deps  请求参数依赖 数组类型 可将参数分解成多个依赖对象
 * @param cb      接口请求完成后 结果回调
 * @param unmount 页面卸载执行方法
 * @returns {function(*): Function} 事件触发方法
 */
const useSubmit = (service:any, cb:any, unmount?:any) => {
  const [submit, setSubmit] = useState({ waitRun: true });
  const result = useMountData(service, [submit], cb, undefined, unmount);
  if (!submit.waitRun && result !== undefined) {
    setSubmit({ waitRun: true });
  }

  const handleSubmit = (params:any) => (e?:any) => {
    e && e.preventDefault();
    setSubmit({
      ...params,
      waitRun: false,
    });
  };

  return handleSubmit;
};

export default useSubmit;
